<?php

namespace App\Models\V1;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Zizaco\Entrust\Traits\EntrustUserTrait;
use App\Scopes\V1\ValidDataScope;
use Illuminate\Database\Eloquent\SoftDeletes;
use Log;

class AdminUser extends Authenticatable
{
    use Notifiable,SoftDeletes,EntrustUserTrait {
        EntrustUserTrait::restore insteadof SoftDeletes;
    }

    /**
     * 模型启动函数
     */
    protected static function boot()
    {
        parent::boot();
        static::addGlobalScope(new ValidDataScope);
    }

    /**
     * 可被批量赋值字段（白名单）
     *
     * @var array
     */
    protected $fillable = [
        'code', 'password', 'is_admin', 'remember_token',
    ];

    /**
     * 限制数组或JSON输出时，隐藏字段
     *
     * @var array
     */
    protected $hidden = [
        'id', 'password', 'remember_token', 'deleted_at',
    ];

    /**
     * 软删除，删除的时间字段
     * @var array
     */
    protected $dates = ['delete_at'];

    /**
     * 获取登录用户对应的维护人员信息
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     *
     */
    public function maintainer()
    {
        return $this->belongsTo(BasicMaintainers::class,'code','code');
    }

    /**
     * 获取关联到用户忘记密码信息
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function forgetPassword()
    {
        return $this->hasMany(ForgetPassword::class, 'code', 'code');
    }

    /***
     * 获取用户的角色
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function roles()
    {
        return $this->belongsToMany(Role::class,'role_user','user_id');
    }

    public function findForPassport($username)
    {
        return AdminUser::where('code', $username)->first();
    }

}
